package top.soulblack.purchasesale.controller.setting.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.Shop;
import top.soulblack.purchasesale.service.system.ShopService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * @Author: 廉雪峰
 * @Date: 2019/5/25 20:34
 * @Version 1.0
 */
@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @RequestMapping("/addShop")
    @ResponseBody
    public Result<Boolean> addCorlor(Shop shop) {
        // 判断是否存在当前颜色
        if (shopService.existShop(shop.getName())) {
            return Result.error(SHOP_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (shop.getSort() > 100 || shop.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        shopService.addShop(shop);
        return Result.success(true);
    }

    @RequestMapping("/deleteShop")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteShop(int shopId) {
        if (shopService.deleteShop(shopId)) {
            return Result.success(true);
        }
        return Result.error(SHOP_DELETE);
    }

    @RequestMapping("/editShop")
    @ResponseBody
    public Result<Boolean> editShop(Shop Shop) {
        if (shopService.updateShop(Shop)) {
            return Result.success(true);
        }
        return Result.error(SHOP_EDIT_ERROR);
    }
}
